package com.happy.zebra.admin.service.impl;

import com.happy.zebra.admin.api.entity.AssistPermission;
import com.happy.zebra.admin.api.vo.AssistPermissionVO;
import com.happy.zebra.admin.mapper.AssistPermissionMapper;
import com.happy.zebra.admin.service.PermissionService;
import com.shycloud.mido.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class PermissionServiceImpl implements PermissionService {

	/** 教辅权限 mapper */
	private AssistPermissionMapper assistPermissionMapper;

	@Override
	public AssistPermissionVO checkAssistPermission(String teacherId) {

		AssistPermissionVO assistPermissionVO = new AssistPermissionVO();
		// 获取此教师教辅权限信息
		List<AssistPermission> assistPermissions = assistPermissionMapper.getAssistPermissions(teacherId);

		if (assistPermissions == null || assistPermissions.size() == 0) {
			// 未开通教辅
			assistPermissionVO.setStatus("0");
			// 剩余时长
			assistPermissionVO.setSurplusTime(0);
		}

		for (AssistPermission assistPermission : assistPermissions) {

			//试用权限
			if ("1".equals(assistPermission.getType())) {
				//类型
				assistPermissionVO.setType("1");
				// 没过期
				if (assistPermission.getEndTime().isAfter(LocalDateTime.now())) {
					//开通了
					assistPermissionVO.setStatus("1");
					//剩余时长
					Duration duration = Duration.between(LocalDateTime.now(), assistPermission.getEndTime());
					assistPermissionVO.setSurplusTime(duration.toDays());
					break;
				} else {
					assistPermissionVO.setStatus("2");
				}
			}

			//购买权限
			if ("2".equals(assistPermission.getType())) {
				assistPermissionVO.setType("2");
				// 没过期
				if (assistPermission.getEndTime().isAfter(LocalDateTime.now())) {
					//开通了
					assistPermissionVO.setStatus("1");
					//剩余时长
					Duration duration = Duration.between(LocalDateTime.now(), assistPermission.getEndTime());
					assistPermissionVO.setSurplusTime(duration.toDays());
					break;
				} else {
					assistPermissionVO.setStatus("2");
				}
			}

		}

		// 试用时长
		assistPermissionVO.setTryDuration(assistPermissionMapper.getTryDuration());

		return assistPermissionVO;
	}


	/**
	 * 激活教辅试用
	 *
	 * @param teacherId
	 * @return boolean
	 * @author nianhua.jiang
	 * @date 2021/2/2 16:30
	 **/
	@Override
	public Map<String, Object> activeAssistPermissionTry(String teacherId) {

		// 判断是否开通过教辅
		AssistPermissionVO assistPermissionVO = checkAssistPermission(teacherId, "1");

		if (!"0".equals(assistPermissionVO.getStatus())) {
			throw new BusinessException("您已试用过该产品，无法重复试用");
		}

		// 动态获取试用时长
		long duration = assistPermissionMapper.getTryDuration();

		// 开通试用
		assistPermissionMapper.insertAssistPermission(teacherId, "1", LocalDateTime.now(), duration, LocalDateTime.now().plusDays(duration));

		Map<String, Object> resMap = new HashMap<>();

		LocalDateTime endTime = LocalDateTime.now().plusDays(duration);
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		resMap.put("endTime", dtf.format(endTime));

		return resMap;

	}

	private AssistPermissionVO checkAssistPermission(String teacherId, String type) {

		AssistPermissionVO assistPermissionVO = new AssistPermissionVO();
		// 获取此教师教辅权限信息
		AssistPermission assistPermission = assistPermissionMapper.getAssistPermission(teacherId, type);

		// 没有教辅权限
		if (assistPermission == null) {
			// 未开通教辅
			assistPermissionVO.setStatus("0");
			// 剩余时长
			assistPermissionVO.setSurplusTime(0);
		} else {

			// 没过期
			if (assistPermission.getEndTime().isAfter(LocalDateTime.now())) {
				//开通了
				assistPermissionVO.setStatus("1");
				//剩余时长
				Duration duration = Duration.between(LocalDateTime.now(), assistPermission.getEndTime());
				assistPermissionVO.setSurplusTime(duration.toDays());
			} else {
				assistPermissionVO.setStatus("2");
			}

		}
		// 试用时长
		assistPermissionVO.setTryDuration(assistPermissionMapper.getTryDuration());

		return assistPermissionVO;
	}

}
